<?php
namespace Mouta\AmistateBundle\Twig;

use Sonata\SeoBundle\Seo\SeoPageInterface;
use Gedmo\Sluggable\Util\Urlizer;
use Mouta\AmistateBundle\Entity\Tags;

class AmistateExtension extends \Twig_Extension
{
    protected $page;
    protected $router;

    public function __construct(SeoPageInterface $page, $router)
    {
        $this->page = $page;
        $this->router = $router;
    }

    public function getFunctions()
    {
        return array(
            'sonata_seo_txt_title'      => new \Twig_Function_Method($this, 'getTextTitle'),
        );
    }
	
    public function getFilters()
    {
        return array(
            'urlize'      => new \Twig_Filter_Method($this,'urlize'),
            'truncate'      => new \Twig_Filter_Method($this,'truncate'),
            'concat_tag'      => new \Twig_Filter_Method($this,'concat_tag'),
            'getLinkByTags'      => new \Twig_Filter_Method($this,'getLinkByTags'),
			'build_tags'  => new \Twig_Filter_Method($this, 'build_tags'),
        );
    }
	
	public function getLinkByTags($tags) {
        $tags = \Mouta\AmistateBundle\Controller\ListController::getTagsOrdenados($tags);
        if(count($tags)==0)
            return $this->router->generate('home');
        if(count($tags)==1)
            return $this->router->generate('list1',array('tag1'=>$tags[0]->getTagGoogle()));
        if(count($tags)==2)
            return $this->router->generate('list2',array('tag1'=>$tags[0]->getTagGoogle(),'tag2'=>$tags[1]->getTagGoogle()));
        if(count($tags)==3)
            return $this->router->generate('list3',array('tag1'=>$tags[0]->getTagGoogle(),'tag2'=>$tags[1]->getTagGoogle(),'tag3'=>$tags[2]->getTagGoogle()));
    }
	
	public function concat_tag($string, $idForm ,$tag) 
	{
        $newTag = new Tags();
        $newTag->setIdForm($idForm);
        $newTag->setTag($string);
        $newTag->setTagGoogle($this->urlize($string));
        $tag[] = $newTag;
        $tags = \Mouta\AmistateBundle\Controller\ListController::getTagsOrdenados($tag);
        if(count($tags)==1)
            return $this->router->generate('list1',array('tag1'=>$tags[0]->getTagGoogle()));
        if(count($tags)==2)
            return $this->router->generate('list2',array('tag1'=>$tags[0]->getTagGoogle(),'tag2'=>$tags[1]->getTagGoogle()));
        if(count($tags)==3)
            return $this->router->generate('list3',array('tag1'=>$tags[0]->getTagGoogle(),'tag2'=>$tags[1]->getTagGoogle(),'tag3'=>$tags[2]->getTagGoogle()));
    }

    public function getTextTitle()
    {
        echo $this->page->getTitle();
    }
	
    public function urlize($text)
    {
		return Urlizer::urlize($text);
    }
	
    public function truncate($string,$len)
    {
		if(strlen($string)<$len) return $string;
        $array = explode(' ', $string);
        $texto = "";
        foreach ($array as $palabra){
            if(strlen($texto)<$len)
            {
                $texto .= $palabra . " ";
            }else{
                break;
            }
        }
        $texto = trim($texto);
		
        if(strlen($string) != strlen($texto))
            return $texto.'...';
        else
            return $texto;
		
    }
	
	public function build_tags($string,$user) 
	{
		$string = str_replace('|', ', ', $string);
        $tags = $user->getTags();
        $string = strip_tags($string);
        $tagsAux = array();
        $tagsEliminar = array();
        foreach($tags as $tag){
            $tagsAux[$tag->getTag()] = $tag;
            foreach ($tags  as $t2){
                if($tag->getTag() == $t2->getTag())
                    continue;
                $expresion = '/\b'.$t2->getTag().'\b/i';
                preg_match($expresion, $tag->getTag(), $coincidencias);
                //var_dump($coincidencias);
                if(count($coincidencias)>0)
                    $tagsEliminar[] = $t2->getTag();
            }
        }
        foreach ($tagsEliminar as $tag){
            unset($tagsAux[$tag]);
        }
        foreach($tagsAux as $tag){
            if($tag->getTagGoogle() == 'hombre' ||$tag->getTagGoogle() == 'mujer')
                continue;
            $expresion = '/\b'.$tag->getTag().'\b/i';
            $replace = $this->router->generate('list2',array('tag1'=> strtolower($user->getOpcionesSexo($user->getSexo())),'tag2'=>$tag->getTagGoogle()));
            $replace = '<a class="small radius button minilink" href="'.$replace.'">'.$tag->getTag().'</a>';
            $string = preg_replace($expresion, $replace, $string,1);
        }
        return $string;
    }

    public function getName()
    {
        return 'amistate_extension';
    }

}